package com.ec50n9.server.common

import com.ec50n9.server.module.common.AppCode
import com.ec50n9.server.module.common.StatusCode

class ApiException(val statusCode: StatusCode, override val message: String?):RuntimeException(message)

fun <R> mayBeNotFound(name: String, isList: Boolean = false, block: () -> R): R =
    try {
        block()
    } catch (e: Exception) {
        throw ApiException(AppCode.OBJECT_NOT_FOUND, if (isList) "无法获取${name}列表" else "${name}不存在")
    }

fun <R> mayBeAlreadyExist(name: String, block: () -> R): R =
    try {
        block()
    } catch (e: Exception) {
        throw ApiException(AppCode.OBJECT_ALREADY_EXISTS, "已存在同名的${name}")
    }
